<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'


//导入接口函数
import { userInfoGetService } from '@/api/user.js'
//导入pinia
import { useUserInfoStore } from '@/stores/user.js'
const userInfoStore = useUserInfoStore();
import { ref } from 'vue'
//获取个人信息
const name=ref()
const getUserInf = async () => {
    let result = await userInfoGetService();
    //存储pinia
    userInfoStore.setInfo(result.data);
    name.value=result.data.name
}
getUserInf()


import { ElMessage, ElMessageBox } from 'element-plus'
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore()
//dropDown条目被点击后，回调的函数
import {useRouter} from 'vue-router'
const router = useRouter()
const handleCommand = (command) => {
    if (command === 'logout') {
//退出登录
ElMessageBox.confirm(
            '你确认退出登录码？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //用户点击了确认
                //清空pinia中的token和个人信息
                // userInfoStore.removeToken
                tokenStore.removeToken()
                //跳转到登录页
                ElMessage.success("退出成功")
                router.push('/login')
            })
            .catch(() => {
                //用户点击了取消
                ElMessage({
                    type: 'info',
                    message: '取消退出',
                })
            })
    } else {
        //路由
        router.push('/user/' + command)
    }
}

</script>
<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <el-sub-menu>
                    <template #title >
                        <el-icon>
                            <Management />
                        </el-icon>
                        <span>公告通知</span>
                    </template>
                    <el-menu-item index="/announcement/getLatestNotice">
                        <el-icon>
                            <Management />
                        </el-icon>
                        <span>最新公告</span>
                    </el-menu-item>
                    <el-menu-item index="/announcement/page">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>查询公告</span>
                    </el-menu-item>
                </el-sub-menu>
                <!-- 项目管理 -->
                <el-sub-menu>
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>项目管理</span>
                    </template>
                    <el-menu-item index='/project'>
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>立项申请</span>
                    </el-menu-item>
                    <el-menu-item index='/project/establishment'>
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>立项列表</span>
                    </el-menu-item>
                    <el-menu-item index="/project/logCount">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>项目进展</span>
                    </el-menu-item>
                    <el-menu-item index="/project/mid_term">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>中期列表</span>
                    </el-menu-item>
                    <el-menu-item index="/project/closing_item">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>结项列表</span>
                    </el-menu-item>
                </el-sub-menu>
                <!--个人中心  -->
                <el-sub-menu>
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/resetPassword">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>当前登录成员：<strong>{{name}}</strong></div>
                <el-dropdown placement="bottom-end"  @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="resetpassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>大创管理系统</el-footer>
        </el-container>
        
    </el-container>
</template>
<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}</style>